package com.haitaoit.chengquan.module.center.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyCollectModel;
import com.haitaoit.chengquan.view.CircleImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/10.
 */
public class MyCollectAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyCollectModel> list;
    private String type;

    public void setType(String type) {
        this.type = type;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyCollectAdapter(Context context) {
        this.context = context;
    }

    public List<MyCollectModel> getList() {
        return list;
    }

    public void setList(List<MyCollectModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_collect_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.collect_type1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyCollectAdapter.this.backCall.deal(R.id.collect_type1, position);
            }
        });
        holder.collect_type2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyCollectAdapter.this.backCall.deal(R.id.collect_type2, position);
            }
        });

        if (type.equals("2")) {
            holder.collect_type1.setVisibility(View.VISIBLE);
            holder.collect_type2.setVisibility(View.GONE);
            ImageLoader.getInstance().displayImage(list.get(position).getHead_portrait(), holder.center_img);
            holder.real_name.setText(list.get(position).getReal_name());
            holder.volume_count.setText("成交量：" + list.get(position).getVolume_count() + "家");
        } else if (type.equals("1")) {
            holder.collect_type2.setVisibility(View.VISIBLE);
            holder.collect_type1.setVisibility(View.GONE);
            ImageLoader.getInstance().displayImage(list.get(position).getTitle_img(), holder.title_img);
            holder.goods_title.setText(list.get(position).getTitle());
            holder.price.setText("¥ " + list.get(position).getPrice());
        }

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.collect_add:
                    case R.id.collect_add2:
                        MyCollectAdapter.this.backCall.deal(R.id.collect_add, position);
                        break;
                }
            }
        };
        holder.collect_add.setOnClickListener(listener);
        holder.collect_add2.setOnClickListener(listener);
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.collect_type1)
        LinearLayout collect_type1;
        @ViewInject(R.id.collect_type2)
        LinearLayout collect_type2;
        @ViewInject(R.id.collect_add)
        ImageView collect_add;
        @ViewInject(R.id.collect_add2)
        ImageView collect_add2;
        @ViewInject(R.id.title_img)
        ImageView title_img;
        @ViewInject(R.id.goods_title)
        TextView goods_title;
        @ViewInject(R.id.price)
        TextView price;
        @ViewInject(R.id.real_name)
        TextView real_name;
        @ViewInject(R.id.volume_count)
        TextView volume_count;
        @ViewInject(R.id.center_img)
        CircleImageView center_img;
    }
}
